package com.yunda.crypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Eenie on 2017/8/24.
 * Email: eeniework@gmail.com
 * Des:
 */

public class PWDEncode {
//
//    public static void main(String[] a) {
//
//
//        System.out.println(KdyMD5Util.toMD5("abcd1234"));
//        try {
//            System.out.println(SHA1("e19d5cd5af0378da05f63f891c7467af"));
//        } catch (DigestException e) {
//            e.printStackTrace();
//        }
//
//
//    }


    public static String SHA1(String decrypt)  {
        try {
            //指定sha1算法
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decrypt.getBytes());
            //获取字节数组
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }

    }


    public static String toMD5(String paramString) {

        StringBuffer localStringBuffer = new StringBuffer();
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramString.getBytes());
            for (int k : localMessageDigest.digest()) {
                int m = k;
                if (k < 0) {
                    m = k + 256;
                }
                if (m < 16) {
                    localStringBuffer.append("0");
                }
                localStringBuffer.append(Integer.toHexString(m));
            }
            return localStringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}


